#include "entity.h"
#include <QDateTime>
#include "../utils/utils.h"

Entity::Entity(QObject *parent)
    : QObject(parent)
{
    this->m_mId = 0;
}

qulonglong Entity::mId()
{
    return this->m_mId;
}

void Entity::setmId(qulonglong mId)
{
    if(this->m_mId != mId)
    {
        this->m_mId = mId;
        emit mIdChanged();
    }
}

QHash<QString, QVariant> Entity::data(QStringList fields)
{
    QHash<QString, QVariant> d;
    bool fieldOrder = !fields.isEmpty();
    int count = fieldOrder?fields.size():this->metaObject()->propertyCount();

    for(int i=0; i<count; i++)
    {
        QString propertyName;
        QVariant propertyValue;

        if(fieldOrder) {
            propertyName = fields[i];
            propertyValue = QObject::property(fields[i].toUtf8());
        } else {
            QMetaProperty property = this->metaObject()->property(i);

            if(!property.isWritable()) continue;
            if(!strcmp(property.name(),"mId")) continue;
            if(!strcmp(property.name(),"objectName")) continue;
            if(!strcmp(property.name(),"parent")) continue;

            propertyName = property.name();
            propertyValue = property.read(this);
        }

        if(propertyName=="QDateTime") {
            d.insert(propertyName,propertyValue.toDateTime().toString("yyyy-MM-dd hh:mm:ss"));
        } else if(propertyName=="QDate") {
            d.insert(propertyName,propertyValue.toDateTime().toString("yyyy-MM-dd"));
        } else {
            d.insert(propertyName,propertyValue);
        }
    }

    return d;
}

QString Entity::keys(QString sep)
{
    QList<QString> keysList = data().keys();
    QString strKeys;

    for(int i=0; i<keysList.size(); i++)
    {
        if(i) strKeys.append(sep);
        strKeys.append(keysList[i]);
    }

    return strKeys;
}

QString Entity::values(QString separator, QString delim, QStringList fields)
{
    QList<QVariant> valuesList = data(fields).values();
    QString strValues;

    for(int i=0; i<valuesList.size(); i++)
    {
        if(i) strValues.append(separator);
        if(valuesList[i].isNull())
        {
            strValues.append("NULL");
        }
        else
        {
            QString strVal = valuesList[i].toString();
            strVal = Utils::escape(strVal, separator, delim);
            strValues.append(delim+strVal+delim);
        }
    }

    return strValues;
}

QString Entity::values()
{
    return values(",","'");
}

QString Entity::keysValues(QString separator, QString delim)
{
    QHash<QString, QVariant> dataList = data();
    QString keysValues;

    for(int i=0; i<dataList.count(); i++)
    {
        QString key = dataList.keys()[i];
        QVariant val = dataList.values()[i];

        if(i) keysValues.append(separator);

        keysValues.append(key);
        keysValues.append("=");

        if(val.isNull())
        {
            keysValues.append("NULL");
        }
        else
        {
            QString strVal = dataList.values()[i].toString();
            strVal = Utils::escape(strVal,separator,delim);
            keysValues.append(delim+strVal+delim);
        }
    }

    return keysValues;
}

QString Entity::keysValues()
{
    return keysValues(",","'");
}

void Entity::setParent(QObject *p)
{
    QObject::setParent(p);
}

QObject* Entity::parent()
{
    return QObject::parent();
}

